package demo;

import org.w3c.dom.Node;

import java.util.*;

public class MyBinaryTree {
    static class TreeNode {
        char val;
        TreeNode left;
        TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }

        @Override
        public String toString() {
            return "val=" + val;
        }
    }

    public TreeNode creatTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
//        E.right = H;
        C.left = F;
        C.right = G;
        return A;
    }

    // 前序遍历
    /*
        为什么在非递归遍历中 都是用 cur = cur.left 来循环？
        因为无论哪种遍历都是先左树再右树
     */
    void preOrderNor(TreeNode root) {
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while(cur != null || !stack.isEmpty()) {
            // 一直遍历 当停止的时候左树就到底了
            while(cur != null) {
                stack.push(cur);
                System.out.print(cur.val);
                cur = cur.left;
            }
            // 弹出 stack 中的元素
            TreeNode top = stack.pop();
            // 接下去就是要遍历右树了
            cur = top.right;
        }
    }

    void preOrder(TreeNode root) {
        System.out.print(root.val);
        if(root.left != null) {
            preOrder(root.left);
        }
        if(root.right != null) {
            preOrder(root.right);
        }
    }
    // 中序遍历

    void inOrderNor(TreeNode root) {
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while(cur != null || !stack.isEmpty()) {
            while(cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            TreeNode top = stack.pop();
            System.out.print(top.val);
            cur = top.right;
        }
    }

    void inOrder(TreeNode root) {

        if(root.left != null) {
            inOrder(root.left);
        }
        System.out.print(root.val);
        if(root.right != null) {
            inOrder(root.right);
        }
    }
    // 后序遍历

    void postOrderNor(TreeNode root) {
        if(root == null) {
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        TreeNode prev = null;
        while(cur != null || !stack.isEmpty()) {
            // 循环结束时候 左树已经遍历完
            while (cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            TreeNode top = stack.peek();
            // 右树要么为空 要么已经打印过了
            if (top.right == null || prev == top.right) {
                System.out.print(top.val);
                stack.pop();
                prev = top;
            } else {
                // 说明还没有打印完
                cur = top.right;
            }
        }
    }

    void postOrder(TreeNode root) {
        if(root.left != null) {
            postOrder(root.left);
        }
        if(root.right != null) {
            postOrder(root.right);
        }
        System.out.print(root.val);
    }

    // 获取树中节点的个数
    int size(TreeNode root) {
        if(root == null) {
            return 0;
        }
        return size(root.left) + size(root.right) + 1;
    }

    // 获取叶子节点的个数    子问题思路-求叶子结点个数
    int getLeafNodeCount(TreeNode root) {
        //表示当前节点为 null
        if(root == null) {
            return 0;
        }
        //表示当前节点存在且为叶子节点
        if(root.left == null && root.right == null) {
            return 1;
        }
        //说明不是叶子节点
        return getLeafNodeCount(root.right) + getLeafNodeCount(root.left);
    }

    // 获取第K层节点的个数
    int getKLevelNodeCount(TreeNode root,int k) {
        if(root == null) {
            return 0;
        }
        /*
            不能 k == 1 && root != null
            因为这样写当不满足的时（如 k = 2, root = null）,就会出现空指针异常
         */
        if(k == 1) {
            return 1;
        }
        return getKLevelNodeCount(root.left, k - 1) +
                getKLevelNodeCount(root.right, k - 1);
    }

    // 获取二叉树的高度
    int getHeight(TreeNode root) {
        if(root == null) {
            return 0;
        }
        //得到左树与右树的较大者 再加上 本身(1)
        return Math.max(getHeight(root.right), getHeight(root.left)) + 1;
    }
    int getHeight2(TreeNode root) {
        if(root == null) {
            return 0;
        }
        //这个与上面有什么区别?
        //这个运行时间长 --> 因为它调用次数多
        return (getHeight2(root.right) > getHeight2(root.left) ? getHeight2(root.right) : getHeight2(root.left)) + 1;
    }

    // 检测值为value的元素是否存在
    TreeNode find(TreeNode root, char val) {
        //找到底了（就是当前这个节点是空树）
        if(root == null) {
            return null;
        }
        //查看当前节点是否符合
        if(root.val == val) {
            return root;
        }
        //找左边
        TreeNode tree = find(root.left, val);
        if(tree != null) {
            return tree;
        }
        //找右边
        tree = find(root.right, val);
        if(tree != null) {
            return tree;
        }
        //没找到
        return null;
    }
    //层序遍历
    void levelOrder(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        if(root == null) {
            return;
        }
        queue.add(root);
        while(!queue.isEmpty()) {
            TreeNode poll = queue.poll();
            //先放左边 再放右边
            if(poll.left != null) {
                queue.add(poll.left);
            }
            if(poll.right != null) {
                queue.add(poll.right);
            }
            System.out.print(poll.val + " ");
        }
    }

    /**
     * 判断一棵树是不是完全二叉树
     * @param root
     * @return
     */
    boolean isCompleteTree(TreeNode root) {
        /*
            当队列弹出 null 的时候，停下来，查看是否有非 null 的元素
            如果有，那么就放回 false 否则就返回 true
            因为层序遍历是从上到下，从左到右的
         */
        Queue<TreeNode> queue = new LinkedList<>();
        if(root == null) {
            return true;
        }
        queue.add(root);
        while(true) {
            TreeNode poll = queue.poll();
            if(poll == null) {
                break;
            }
            queue.add(poll.left);
            queue.add(poll.right);
        }
        while(!queue.isEmpty()) {
            if(queue.poll() != null) {
                return false;
            }
        }
        return true;
    }

}
